﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Tanmia.Core;
using System.Windows.Controls.Ribbon;

namespace Tanmia.Windows.RepositoryScenario
{
    /// <summary>
    /// Interaction logic for RepositoryEdit.xaml
    /// </summary>
    public partial class RepositoryEdit : RibbonWindow
    {
        RepositoryViewModel RepositoryVM = new RepositoryViewModel();


        public RepositoryEdit()
        {
            InitializeComponent();

            DataContext = RepositoryVM;
        }


        private void ShowValidationTool(string error)
        {
            vatError.Label = error;
            vatError.Visibility = Visibility.Visible;
        }

        private bool Save()
        {
            RepositoryVM.Entity.Validate();

            if (!RepositoryVM.Entity.IsValid())
            {
                ShowValidationTool(RepositoryVM.Error);
                return false;
            }
            else
            {
                try
                {
                    RepositoryVM.Save();

                    if (vatError.Visibility == Visibility.Visible)
                        vatError.Visibility = Visibility.Collapsed;

                    return true;

                }
                catch (Exception ex)
                {
                    ShowValidationTool(ex.Message);
                    return false;
                }
            }
        }


        private void RibbonWindow_Loaded(object sender, RoutedEventArgs e)
        {
            dtpCreateDate.SelectedDate = DateTime.Now;

            cmbRecord.IsEnabled = false;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            Save();
        }

        private void btnSaveAndNew_Click(object sender, RoutedEventArgs e)
        {
            if (Save())
                btnClear_Click(sender, e);
        }

        private void btnSaveAndPrint_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnPerviewPrint_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnSetupPrint_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            RepositoryVM.Entity = new Repository();
            dtpCreateDate.SelectedDate = DateTime.Now;

            if (vatError.Visibility == Visibility.Visible)
                vatError.Visibility = Visibility.Collapsed;
        }

        private void btnSaveAndClose_Click(object sender, RoutedEventArgs e)
        {
            if (Save())
                Close();
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void btnLast_Click(object sender, RoutedEventArgs e)
        {
            RepositoryVM.Last();
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            RepositoryVM.Next();
        }

        private void btnPrevious_Click(object sender, RoutedEventArgs e)
        {
            RepositoryVM.Previous();
        }

        private void btnFirst_Click(object sender, RoutedEventArgs e)
        {
            RepositoryVM.First();
        }

        private void btnOptions_Click(object sender, RoutedEventArgs e)
        {

        }

    }
}
